var comPageNum = 1;//分页公共页码
var dataSource = new Array();
//table的数据源
var table; //table对象
dataSource = [];

function goodsList(data) {
    if(dataSource.length > 0){
        dataSource.splice(0, dataSource.length); //清空数组
    }
    if(data.data != null){
        $.each(data.data.list, function(i, field) {
            var rowData = new Array();
            rowData.push(field.sku_code);
            rowData.push(field.sku_name);
            rowData.push(field.specifications);
            rowData.push(field.product_code);
            rowData.push(field.old_sku_name);
            rowData.push(field.old_specifications);
            rowData.push(field.brand_name);
            rowData.push(field.unit);
            rowData.push(field.supplier_name);
            rowData.push(field.customer_name);
            rowData.push(field.cost_price);
            rowData.push(field.price);
            var time = format(field.creation_date);
            rowData.push(time);
            rowData.push(field.sku_id);
            rowData.push(field.virtual_stock);
            rowData.push(field.pro_useable);
            rowData.push(field.actual_stock);

            dataSource.push(rowData);
        });
    }

    initDataTable(dataSource);

    function initDataTable(dataSource) {
        var dttable = $('#datatable').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#datatable").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": true,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": [50, 25, 10, 5],
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
                "sProcessing": "<img src='./loading.gif' />"
            },
            data: dataSource,
            columnDefs: [
                {
                "visible": true,
                "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = row[3] + '<br/>' + row[4] + '<br/>' + row[5];
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = row[6];
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = row[7];
                        return val;
                    },
                    "targets": 2
                },

                {
                    "render": function(data, type, row, meta) {
                        var customer_name= ""
                        if(row[9]!=null){
                            customer_name=row[9]
                        }else {
                            customer_name= ""
                        }
                        var val = '<span id="' + row[13] + '" onmouseover="showAll(' + row[13] + ',' + row[0] + ')" onmouseout="hideAll(' + row[0] + ')" style="overflow: hidden; text-overflow: ellipsis; display: -webkit-box; -webkit-line-clamp:2; line-clamp:2; -webkit-box-orient: vertical;">' + customer_name + '</span>';
                        val += '<div class="' + row[0] + '" style="position:absolute;right: 300px; background: #fff;padding:10px;border:1px solid #ccc;display:none;"></div>';
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '成本价：' + row[10] + '元<br/>VIP价：' + row[11] + '元';
                        return val;
                    },
                    "targets": 4
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[14]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 5
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[16]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 6
                },
                {
                    "render": function (data, type, row, meta) {
                        var val =""
                        if ( row[15] == 0) {
                            val = '启用'
                        }else  if ( row[15] == 1) {
                            val = '禁用'
                        }
                        return val;
                    },
                    "targets": 7
                },
                {
                    "render": function(data, type, row, meta) {
                        var date = row[12];
                        var split = date.split(" ");
                        var val = split[0];
                        return val;
                    },
                    "targets": 8
                },
                {
                    "render": function(data, type, row, meta) {
                        var val = '<a onclick="procure_edit(\'临采商品详情\', \'procureGoodsManageDetails.html\', ' + row[13] + ')" class="powerButton 1000001004725699">编辑</a>';
                        return val;
                    },
                    "targets": 9
                },
            ],
        });
    };
    power();
}
function getTemporaryGoodsList(pageNum) {
    comPageNum = pageNum;
    $.ajax({
        contentType: 'application/json',
        url: root.goodsInter.goods_temporaryProductList,
        cache: false, //禁用缓存
        type: "POST",
        data: JSON.stringify({
            pageSize: 20,
            nowPage: pageNum,
            model: {
                skuCode: $("#skuCode").val(),
                code: $("#code").val(),
                skuName: $("#productName").val(),
                oldSkuName: $("#oldProducrName").val(),
                specifications: $("#specificationsName").val(),
                oldSpecifications: $("#oldSpecificationsName").val(),
                brand: $("#brand").val(),
                inquiryCustomer: $("#inquiryCustomer").val(),
                createStart: $("#timemin").val(),
                createEnd: $("#timemax").val(),
                "proUseable":$("#proUseable").val()
            }
        }),
        success: function(data) {
            if(data.data.list.length==0&&comPageNum!=1){
                getTemporaryGoodsList(comPageNum-1);
                return;
            }
            goodsList(data);
            totalPage = data.data.totalPage;
            $('.M-box3').pagination({
                pageCount: totalPage,
                current:comPageNum,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function(api) {
                    comPageNum = api.getCurrent();
                    getTemporaryGoodsList(api.getCurrent());
                }
            });
        }
    });
}
function procure_edit(title, url, row) {
    /*删除添加spu图片的缓存，防止刷新页面后缓存不消失*/
    sessionStorage.removeItem("spuImgCache");
    sessionStorage.removeItem("procureImgCache");
    sessionStorage.removeItem("brand_name");
    sessionStorage.removeItem("brand_id");
    sessionStorage.removeItem("attrObjCanch");
    sessionStorage.removeItem("attrId");
    window.session_storage.setItem('sku_id', row);
    var index = layer.open({
        type: 2,
        title: title,
        content: url
    });
    layer.full(index);
}
function showAll(code, id) {
    $("." + id).show();
    $("." + id).html($("#" + code).html());
}
function hideAll(id) {
    $("." + id).hide();
}